<?php

namespace App\HttpController\Api\Admin;

use Exception;

/**
 * 控制器
 * @author tirin
 * @since: 2021/08/24
 * Class Student
 */
class Common extends AdminBase
{

    public $req = null;

    public function __construct()
    {
        parent::__construct();
    }
    public static $levelArr = [
        '1' => 'DormBuilding',
        '2' => 'DormFloor',
        '3' => 'DormRoom',
        '4' => 'DormStudent',
    ];
    public static function getModel($table){
        $className = "\App\Model\Admin\\$table";
        return new $className();
    }
    public function getListByLevel()
    {
        $param = $this->request()->getRequestParam();
        if (!in_array($param['level'], [1, 2, 3, 4])) {
            return $this->fail(400, '层级不正确');
        }
        //实例化模型
        $obj = self::getModel(self::$levelArr[$param['level']]);

        $this->success($obj->getList($param), 200);
    }
    public function getListByTable()
    {
        $param = $this->request()->getRequestParam();
        if (!$param['table']) {
            return $this->fail(400, 'table不能我为空');
        }
        //实例化模型
        $obj = self::getModel($param['table']);

        $this->success($obj->getList($param), 200);
    }
    /**
     * 备用
     */
    public function getChildrenNodeBackUp()
    {
        $param = $this->request()->getRequestParam();
        if ($param['level'] == 4) {
        } else {
            $res = new \App\Model\Admin\Dorm();
            $where = ['p_id' => (int)$param['id']];
            $this->success($res->where(['status' => 1])->where($where)->field('id,title label,id children,id uuid')->select()->toArray(), 200);
        }
    }
    /**
     * 获取节点
     */
    public function getChildrenNode()
    {
        $param = $this->request()->getRequestParam();

        if (!in_array($param['level'], [1, 2, 3, 4])) {
            return $this->fail(400, '层级不正确');
        }
        //实例化模型
        $obj = self::getModel(self::$levelArr[$param['level']]);

        $where = ['p_id' => (int)$param['id']];

        if ($param['level'] == 1) {
            $obj = new \App\Model\Admin\DormBuilding();
            //暂时
            $where = [];
        }

        return $this->success($obj->where(['status' => 1])->where($where)->field('id,name label,id children,id uuid')->select()->toArray(), 200);
    }
    //获取页面的cols
    public function getCols()
    {
        $param = $this->request()->getRequestParam();

        if (!in_array($param['level'], [1, 2, 3, 4])) {
            return $this->fail(400, '层级不正确');
        }

        $obj = self::getModel(self::$levelArr[$param['level']]);
        return $this->success($obj->cols, 200);
    }
}
